package com.cocotingo.snail.text;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.RenderingHints;

import com.cocotingo.snail.GraphicsX;
import com.cocotingo.snail.Vector;
import com.cocotingo.snail.View;

public class TextView extends View {
	
	private String text;
	private Color color;
	private Font font;
	private int alignment;
	
	public static final int HORIZONTAL_LEFT = 1 << 0;
	public static final int HORIZONTAL_CENTER = 1 << 1;
	public static final int HORIZONTAL_RIGHT = 1 << 2;
	public static final int VERTICAL_TOP = 1 << 3;
	public static final int VERTICAL_CENTER = 1 << 4;
	public static final int VERTICAL_BOTTOM = 1 << 5;
	
	final static Font DEFAULT_FONT = new Font(Font.DIALOG, Font.PLAIN, 11);

	public TextView() {
		this(0, 0, 0, 0);
	}
	
	public TextView(int left, int top, int width, int height) {
		super(left, top, width, height);
		text = "";
		color = Color.black;
		font = DEFAULT_FONT;
		alignment = HORIZONTAL_LEFT | VERTICAL_CENTER;
	}
	
	public String getText() {
		return text;
	}
	
	public Color getTextColor() {
		return color;
	}
	
	public Font getFont() {
		return font;
	}
	
	public int getAlignment() {
		return alignment;
	}
	
	public void setText(String text) {
		this.text = text;
		setNeedsRepaint();
	}
	
	public void setTextColor(Color color) {
		this.color = color;
		setNeedsRepaint();
	}
	
	public void setFont(Font font) {
		this.font = font;
		setNeedsRepaint();
	}
	
	public void setAlignment(int alignment) {
		int curVerticalFlag = this.alignment & (VERTICAL_TOP | VERTICAL_CENTER | VERTICAL_BOTTOM);
		int curHorizontalFlag = this.alignment & (HORIZONTAL_LEFT | HORIZONTAL_CENTER | HORIZONTAL_RIGHT);
		int newVerticalFlag = alignment & (VERTICAL_TOP | VERTICAL_CENTER | VERTICAL_BOTTOM);
		int newHorizontalFlag = alignment & (HORIZONTAL_LEFT | HORIZONTAL_CENTER | HORIZONTAL_RIGHT);
		if (newVerticalFlag != 0 && newHorizontalFlag == 0) {
			this.alignment = newVerticalFlag | curHorizontalFlag;
		} else if (newVerticalFlag == 0 && newHorizontalFlag != 0) {
			this.alignment = curVerticalFlag | newHorizontalFlag;
		} else if (newVerticalFlag != 0 && newHorizontalFlag != 0) {
			this.alignment = alignment;
		}
		setNeedsRepaint();
	}
	
	public Vector getPreferredSize() {
		GraphicsX g = getTemporaryGraphicsContext();
		FontMetrics metrics = g.getFontMetrics(font);
		Vector vector = new Vector(metrics.stringWidth(text), metrics.getAscent() + metrics.getDescent());
		g.dispose();
		return vector;
	}
	
	private boolean antialiased = true;
	
	public boolean isAntialiased() {
		return antialiased;
	}
	
	public void setAntialiased(boolean antialiased) {
		this.antialiased = antialiased;
		setNeedsRepaint();
	}
	
	@Override
	public void repaintView(GraphicsX g) {
		if (!antialiased) {
			g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
		}
		g.setFont(font);
		g.setColor(color);
		FontMetrics metrics = g.getFontMetrics();
		int top;
		if ((alignment & VERTICAL_TOP) != 0) {
			top = metrics.getAscent();
		} else if ((alignment & VERTICAL_BOTTOM) != 0) {
			top = getHeight() - metrics.getDescent();
		} else {
			top = (getHeight() + metrics.getAscent() - metrics.getDescent()) / 2;
		}
		if ((alignment & HORIZONTAL_LEFT) != 0) {
			g.drawString(text, 0, top);
		} else if ((alignment & HORIZONTAL_RIGHT) != 0) {
			g.drawString(text, getWidth() - metrics.stringWidth(text), top);
		} else {
			g.drawString(text, (getWidth() - metrics.stringWidth(text)) / 2, top);
		}
	}
	
	@Override
	public String toString() {
		return text;
	}
	
}
